﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Gargoyle.ZZT
{
    public delegate bool ItemDelegate(Game g, Int16 amount);

    static class Items
    {
        public static bool Ammo(Game g, Int16 amount)
        {
            return PerformTransaction(ref g.Info.Ammo, amount);
        }

        public static bool Gems(Game g, Int16 amount)
        {
            return PerformTransaction(ref g.Info.Gems, amount);
        }

        public static bool Health(Game g, Int16 amount)
        {
            return PerformTransaction(ref g.Info.Health, amount);
        }

        private static bool PerformTransaction(ref Int16 value, Int16 amount)
        {
            bool success = false;
            Int16 finalAmount;

            unchecked
            {
                finalAmount = (Int16)(value + amount);
                if (finalAmount < 0)
                {
                    success = false;
                }
                else
                {
                    success = true;
                    value = finalAmount;
                }
            }

            return success;
        }

        public static bool Score(Game g, Int16 amount)
        {
            return PerformTransaction(ref g.Info.Score, amount);
        }

        public static bool Stones(Game g, Int16 amount)
        {
            if (g.Parameters.StonesEnabled)
                return PerformTransaction(ref g.Info.Stones, amount);
            return false;
        }

        public static bool Time(Game g, Int16 amount)
        {
            return PerformTransaction(ref g.Info.TimePassed, amount);
        }

        public static bool Torches(Game g, Int16 amount)
        {
            if (g.Parameters.TorchesEnabled)
                return PerformTransaction(ref g.Info.Torches, amount);
            return false;
        }

    }
}
